package com.sywb.chuangyebao.view;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.sywb.chuangyebao.Constants;
import com.sywb.chuangyebao.R;
import com.umeng.analytics.MobclickAgent;

import org.bining.footstone.log.Logger;
import org.bining.footstone.utils.SharedUtils;
import org.bining.footstone.utils.StatusBarUtils;
import org.bining.footstone.utils.ToastUtils;

/**
 * 插屏广告页
 */
public class InterstitialActivity extends Activity {

    WebView webContent;

    private String webUrl;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_interstitial);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            StatusBarUtils.setStatusBarMode(this, true);
        }

        StatusBarUtils.setColorByTransparent(this);

        if (savedInstanceState != null) {
            webUrl = savedInstanceState.getString("p0");
        } else {
            webUrl = getIntent().getStringExtra("p0");
        }

        webContent = (WebView) findViewById(R.id.web_content);

        initWebView();
        webContent.addJavascriptInterface(new JsInteration(), "android");
        webContent.loadUrl(webUrl);
        webContent.setBackgroundColor(0);

    }

    /**
     * 初始化WebView
     */
    @SuppressLint("SetJavaScriptEnabled")
    private void initWebView() {

        /**
         * setAllowFileAccess 启用或禁止WebView访问文件数据 setBlockNetworkImage 是否显示网络图像
         * setBuiltInZoomControls 设置是否支持缩放 setCacheMode 设置缓冲的模式
         * setDefaultFontSize 设置默认的字体大小 setDefaultTextEncodingName 设置在解码时使用的默认编码
         * setFixedFontFamily 设置固定使用的字体 setJavaSciptEnabled 设置是否支持Javascript
         * setLayoutAlgorithm 设置布局方式 setLightTouchEnabled 设置用鼠标激活被选项
         * setSupportZoom 设置是否支持变焦
         * */
        WebSettings settings = webContent.getSettings();
        settings.setJavaScriptEnabled(true);// 设置支持javascript脚本
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        // 1.NARROW_COLUMNS：可能的话使所有列的宽度不超过屏幕宽度；2.NORMAL：正常显示不做任何渲染；3.SINGLE_COLUMN：把所有内容放大webview等宽的一列中
        // settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);// 排版适应屏幕
        settings.setDisplayZoomControls(false);
        settings.setAllowFileAccess(true); // 启用或禁止WebView访问文件数据
        settings.setBuiltInZoomControls(false); //  隐藏缩放按钮
        settings.setSupportZoom(true); // 支持缩放
        // settings.setUseWideViewPort(true);// 可任意比例缩放
        settings.setLoadWithOverviewMode(true);//setUseWideViewPort方法设置webview推荐使用的窗口；设置webview加载的页面的模式
        settings.setGeolocationEnabled(true);// 启用地理定位
        settings.setDomStorageEnabled(true);
        settings.setSaveFormData(true);// 保存表单数据
        // settings.setSavePassword(true);
        // settings.setTextZoom(200);
        // settings.setMixedContentMode(1);
//         settings.setAcceptThirdPartyCookies();
        settings.setAllowFileAccess(true);
        settings.setAllowContentAccess(true);
        settings.setAllowFileAccessFromFileURLs(true);
        settings.setAllowUniversalAccessFromFileURLs(true);

        settings.setBlockNetworkImage(false);//解决图片不显示
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }

        webContent.setWebChromeClient(new WebChromeClient());
        webContent.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return false;
            }
        });
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(this.getClass().getName());
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(this.getClass().getName());
        MobclickAgent.onPause(this);
    }

    public class JsInteration {

        @JavascriptInterface
        public void getLocalProcess(int type, String data) {
            Logger.e("type:" + type + " data:" + data);
            if (type != 0) {
                Intent intent = new Intent(InterstitialActivity.this, WebActivity.class);
                intent.putExtra("p0", "");
                intent.putExtra("p1", data);
                startActivity(intent);
            }
            finish();
        }

        public boolean isLogin() {
            return SharedUtils.getBoolean(Constants.USERISLOGIN, false);
        }

        public String getUserId() {
            return SharedUtils.getString(Constants.USEROPENID, "");
        }

        public boolean isBindMobile() {
            return SharedUtils.getBoolean(Constants.USERMOBILEVERIFY, false);
        }

    }

}
